
#########################################################################

# Copyright (C) Flavio Piccinelli 2012 <flavio.picci@gmail.com>

#########################################################################

# Utils.rb
# This file is part of "AriaAperta".

# AriaAperta is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# AriaAperta is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AriaAperta.  If not, see <http://www.gnu.org/licenses/>.

#########################################################################
#########################################################################


module Utils

	class MySSLSocket < OpenSSL::SSL::SSLSocket

		def initialize(io, ctx)
			super(io, ctx)
		end
		def gets_all()
			lineOut = String.new
			term = String.new
			loop do
				lineIn = self.gets
				if lineIn.nil?
					puts "connection closed by server"
					exit 1
				end
				case lineIn.chomp
				when "EOL"
					term = "EOL"
					break
				when "EOF"
					term = "EOF"
					break
				when "RET"
					term = "RET"
					break
				else
					lineOut += lineIn
				end
			end
			return lineOut, term
		end
	end

	def usage()
		puts "Usage:"
		puts "ruby ariaCli.rb <req_type> <req_opts>"
		puts "req_type:"
		puts "	0 for VoIP service"
		puts "	1 for requesting a virtual machine"
		puts "	2 to send ack"
		puts "	3 to stop virtual machines <vm id|range|'all'>"
		exit -1
	end

	def authenticate(sslSocket, username, passwd)

# 		lineIn = Array.new(2)
# 		lineIn[1] = "RET"
		
		begin
			lineIn = sslSocket.gets_all
			if username.nil?
				$stdout.print lineIn[0].chomp
				$stdout.flush
				username = $stdin.gets
			end
			sslSocket.puts username
			username = nil
			lineIn = sslSocket.gets_all
			if passwd.nil?
				$stdout.print lineIn[0].chomp
				$stdout.flush
				passwd = $stdin.gets
			end
			sslSocket.puts passwd
			passwd = nil
			lineIn = sslSocket.gets_all
			puts lineIn[0]
		end until lineIn[1] != "RET"

	end
		

	def req_voip(sslSocket)

		lineIn = sslSocket.gets_all
		if lineIn[1] == "EOF"
			puts lineIn[0]
			sslSocket.close
			exit 0
		end
		host_arr = lineIn[0].chomp.split("+")
		puts host_arr

		ping_arr = Array.new
		host_arr.each do |host|
			host =  host.chomp
			ping = `ping #{PING_OPTS} #{host} #{PING_ACTS}`.to_f
			if ping != 0
				ping_arr << [host, ping.to_f]
			end
		end
		host = ping_arr.min_by{|k,v| v}[0]

		sslSocket.puts host
		lineIn = sslSocket.gets_all
		puts lineIn[0]
		
	end

	def req_vm(sslSocket)

		lineIn = sslSocket.gets_all
		puts lineIn[0]
		if lineIn[1] == "EOF"
			exit 0
		end

		begin
			img_id = $stdin.gets
			sslSocket.puts img_id
			lineIn = sslSocket.gets_all
			case lineIn[1]
			when "EOF"
				puts lineIn[0]
				exit 0
			when "RET"
				puts lineIn[0]
				# 		puts lineIn[1]
			else
				host_arr = lineIn[0].chomp.split("+")
			end
		end until lineIn[1] != "RET"

		puts "\nlooking for host...\n"

		puts host_arr

		ping_arr = Array.new
		host_arr.each do |host|
			host =  host.chomp
			ping = `ping #{PING_OPTS} #{host} #{PING_ACTS}`
			ping_arr << [host, ping.to_f]
		end

		host = ping_arr.min_by{|k,v| v}[0]
		# puts host

		sslSocket.puts host

		puts "starting vm...\n"

		lineIn = sslSocket.gets_all
		puts lineIn[0]
	end

	def req_ack(sslSocket)
		lineIn = sslSocket.gets_all
		puts lineIn[0]
	end

	def req_close(sslSocket, vm_ids)
		sslSocket.puts vm_ids
		lineIn = sslSocket.gets_all
		puts lineIn[0]
	end
	
end
